package org.javaforever.gatescore.shiroauth.verb;

import java.util.ArrayList;
import java.util.List;

import org.javaforever.gatescore.core.FrontCodeBlock;
import org.javaforever.gatescore.core.FrontDomain;
import org.javaforever.gatescore.core.FrontMethod;
import org.javaforever.gatescore.core.FrontVerb;
import org.javaforever.gatescore.core.Statement;
import org.javaforever.gatescore.core.Writeable;
import org.javaforever.gatescore.core.WriteableUtil;
import org.javaforever.gatescore.utils.StringUtil;

public class ChangePasswordUser extends FrontVerb {

	public ChangePasswordUser(FrontDomain domain) {
		super(domain);
		this.verbName = "ChangePassword"+this.domain.getStandardName();
	}

	@Override
	public FrontMethod generateControllerMethod() throws Exception {
		if (this.denied)
			return null;
		else {
			FrontMethod method = new FrontMethod();
			method.setStandardName("change"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+StringUtil.capFirst(this.domain.getStandardName()));
			List<Writeable> sList = new ArrayList<Writeable>();
			sList.add(new Statement(1000L,2,"change"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+this.domain.getCapFirstDomainName()+"() {"));
			sList.add(new Statement(2000L,3,"var "+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+" = this.form."+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()));
			sList.add(new Statement(3000L,3,"var "+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+" = this.form."+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()));
			sList.add(new Statement(4000L,3,"var confirm"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+" = this.form.confirm"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()));
			sList.add(new Statement(5000L,3,"if ("+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+" !== confirm"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+") {"));
			if (this.domain.getLanguage().equalsIgnoreCase("english")) {
				sList.add(new Statement(6000L,4,"this.$alert('Passwords did not match!')"));
			}else {
				sList.add(new Statement(6000L,4,"this.$alert('密码不匹配！')"));
			}
			sList.add(new Statement(7000L,4,"return"));
			sList.add(new Statement(8000L,3,"}"));
			sList.add(new Statement(9000L,3,"var old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+" = "+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()));
			sList.add(new Statement(10000L,3,"change"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+this.domain.getCapFirstDomainName()+"("+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+", old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+", "+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+").then(response => {"));
			sList.add(new Statement(11000L,4,"this.reload()"));
			sList.add(new Statement(12000L,3,"})"));
			sList.add(new Statement(13000L,2,"},"));

			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}

	@Override
	public FrontMethod generateApiMethod() throws Exception {
		FrontMethod method = new FrontMethod();
		method.setStandardName("change"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+this.domain.getLowerFirstDomainName());
		method.addAdditionalImport("import sha1 from 'sha1'");
		List<Writeable> sList = new ArrayList<Writeable>();
		
		sList.add(new Statement(1000L,0,"export function change"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+this.domain.getCapFirstDomainName()+"("+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+", old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+", "+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+") {"));
		sList.add(new Statement(2000L,1,"return request({"));
		sList.add(new Statement(3000L,2,"url: '"+this.domain.getLowerFirstDomainName()+this.domain.getControllerNamingSuffix()+"/change"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+this.domain.getCapFirstDomainName()+"',"));
		sList.add(new Statement(4000L,2,"method: 'post',"));
		if (this.domain.isUseOnlyJson() == true) {
			sList.add(new Statement(5000L,2,"data: { "+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+": "+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+", old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+": sha1(old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+"), "+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+": sha1("+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+") }"));
		} else {
			sList.add(new Statement(5000L,2,"data: qs.stringify({ "+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+": "+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+", old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+": sha1(old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+"), "+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+": sha1("+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+") })"));
		}
		sList.add(new Statement(6000L,1,"})"));
		sList.add(new Statement(7000L,0,"}"));
		
		method.setMethodStatementList(WriteableUtil.merge(sList));
		return method;
	}

	@Override
	public FrontCodeBlock generateRouteBlockBlock() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public FrontMethod generateControllerMethod(String indexSuffix) throws Exception {
		return generateControllerMethod();
	}
}
